﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

using Dx.SqlAccess;
using Dx.Common;

namespace Dx.SysAdmin
{
    /// <summary>
    ///模块编号：CS002
    ///功能描述：实现系统预警接口
    ///创 建 人：杨秀东
    ///创建日期：2008-10-24
    /// </summary>
    public class SysWarm:ISysWarm
    {
        /// <summary>
        /// 添加预警数据
        /// </summary>
        private const string Pro_Insert_SysWarm = "Pro_Insert_SysWarm";

        /// <summary>
        /// 更新全部数据
        /// </summary>
        private const string Sql_Update_SysWarm = " UPDATE sysWarm" +
                                                     " SET GotoUrl = @GotoUrl," +
                                                         " VoiceFile = @VoiceFile" +
                                                   " WHERE WarmValue = @WarmValue";

        /// <summary>
        /// 删除全部数据
        /// </summary>
        private const string Sql_Delete_SysWarm = "DELETE FROM sysWarm WHERE WarmValue = @WarmValue";

        /// <summary>
        /// 获取全部数据
        /// </summary>
        private const string Sql_Select_SysWarm = "SELECT * FROM sysWarm ORDER BY WarmSn desc";

        /// <summary>
        /// 只获取开通的预警数据
        /// </summary>
        private const string Sql_Open_SysWarm = "SELECT * FROM sysWarm WHERE State = 1 ORDER BY WarmSn desc";

        /// <summary>
        /// 降低预警优先级
        /// </summary>
        private const string Sql_Update_SysWarm_Desc = "UPDATE sysWarm SET WarmSn = WarmSn - 1" +
                                                       " WHERE WarmValue = @WarmValue";

        /// <summary>
        /// 提高预警优先级
        /// </summary>
        private const string Sql_Update_SysWarm_Asc = "UPDATE sysWarm SET WarmSn = WarmSn + 1" +
                                                      " WHERE WarmValue = @WarmValue";

        /// <summary>
        /// 获取预警值
        /// </summary>
        private const string Sql_Get_WarmValue = " SELECT TOP 1 WarmValue FROM sysWarm" +
                                                  " WHERE State = 1 AND WarmNum > 0" +
                                               " Order by WarmSn desc";

        /// <summary>
        /// 开通/禁用预警
        /// </summary>
        private const string Pro_Enabled_SysWarm = "Pro_Enabled_SysWarm";

        /// <summary>
        /// 获取某个预警
        /// </summary>
        private const string Sql_GetOne_SysWarm = "SELECT * FROM sysWarm WHERE WarmValue = @WarmValue";

        #region ISysWarm 成员

        /// <summary>
        /// 添加系统预警
        /// </summary>
        /// <param name="warmInfo">预警实体</param>
        /// <returns>-1失败或重复，0异常，1成功</returns>
        public int AddSysWarm(SysWarmInfo warmInfo)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(4);
            param.Add("@GotoUrl", warmInfo.GotoUrl);
            param.Add("@VoiceFile", warmInfo.VoiceFile);
            param.Add("@WarmValue", warmInfo.WarmValue);
            param.Add("@WarmName", warmInfo.WarmName);

            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.StoredProcedure, Pro_Insert_SysWarm, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            return intNum;
        }

        /// <summary>
        /// 删除一个预警数据
        /// </summary>
        /// <param name="id">预警编号</param>
        /// <returns></returns>
        public bool DelSysWarm(string id)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(1);
            param.Add("@WarmValue", id);
            
            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.Text, Sql_Delete_SysWarm, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            if (intNum == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 编辑预警数据
        /// </summary>
        /// <param name="warmInfo">预警实体</param>
        /// <returns></returns>
        public bool EditSysWarm(SysWarmInfo warmInfo)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(3);
            param.Add("@GotoUrl", warmInfo.GotoUrl);
            param.Add("@VoiceFile", warmInfo.VoiceFile);
            param.Add("@WarmValue", warmInfo.WarmValue);

            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.Text, Sql_Update_SysWarm, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            if (intNum == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获取全部预警数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllSysWarm()
        {
            DataSet ds = new DataSet();
            // 执行语句
            ds = SqlDataAccess.GetDataSet(SqlDataAccess.SQLConnString1, CommandType.Text, Sql_Select_SysWarm, null);
            return ds;
        }

        /// <summary>
        /// 只获取开放的预警数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetOpenSysWarm()
        {
            DataSet ds = new DataSet();
            // 执行语句
            ds = SqlDataAccess.GetDataSet(SqlDataAccess.SQLConnString1, CommandType.Text, Sql_Open_SysWarm, null);
            return ds;
        }

        /// <summary>
        /// 预警优先级降低
        /// </summary>
        /// <param name="id">预警编号</param>
        /// <returns>true成功，false失败</returns>
        public bool SetSysWarmDesc(string id)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(1);
            param.Add("@WarmValue", id);

            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.Text, Sql_Update_SysWarm_Desc, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            if (intNum == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 预警优先级提高
        /// </summary>
        /// <param name="id">预警编号</param>
        /// <returns>true成功，false失败</returns>
        public bool SetSysWarmAsc(string id)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(1);
            param.Add("@WarmValue", id);
            
            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.Text, Sql_Update_SysWarm_Asc, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            if (intNum == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 根据预警优先级得到某个预警值
        /// </summary>
        /// <returns>string OR null</returns>
        public string GetOneWarmValue()
        {
            object returnValu;
            
            // 执行语句
            returnValu = SqlDataAccess.ExecuteScalar(SqlDataAccess.SQLConnString1, CommandType.Text, Sql_Get_WarmValue, null);
            if (returnValu != null)
                return Convert.ToString(returnValu);
            else
                return null;
        }

        /// <summary>
        /// 开通/禁用预警
        /// </summary>
        /// <param name="id">预警编号</param>
        /// <returns>true成功，false失败</returns>
        public bool EnabledSysWarm(string id)
        {
            int intNum = 0;
            // 定义参数
            ParameterCollection param = new ParameterCollection(1);
            param.Add("@WarmValue", id);

            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.StoredProcedure, Pro_Enabled_SysWarm, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            if (intNum == 1)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获取某个预警业务实体
        /// </summary>
        /// <param name="id">预警编号</param>
        /// <returns>SysWarmInfo</returns>
        public SysWarmInfo GetOneWarmInfo(string id)
        {
            SysWarmInfo warmInfo = new SysWarmInfo();
            SqlDataReader dr = null;
            // 定义参数
            ParameterCollection param = new ParameterCollection(1);
            param.Add("@WarmValue", id);
            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                dr = SqlDataAccess.ExecuteReader(conn, CommandType.Text, Sql_GetOne_SysWarm, param);
                if (dr.Read())
                {
                    warmInfo.WarmValue = int.Parse(dr["WarmValue"].ToString());
                    warmInfo.WarmName = dr["WarmName"].ToString();
                    warmInfo.VoiceFile = dr["VoiceFile"].ToString();

                    warmInfo.State = Convert.ToBoolean(dr["State"].ToString());
                    warmInfo.GotoUrl = dr["GotoUrl"].ToString();
                    warmInfo.WarmNum = int.Parse(dr["WarmNum"].ToString());

                    warmInfo.WarmSn = int.Parse(dr["WarmSn"].ToString());
                }
                else
                {
                    warmInfo = null;
                }
                conn.Close();
            }

            // 返回结果
            return warmInfo;
        }

        #endregion
    }
}
